package org.example.game.history;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/22 22:42
 */
public class Solution100346 {

    public static void main(String[] args) {
        Solution100346 solution = new Solution100346();
        System.out.println(solution.minOperations(new int[]{0,1,0,1}));
    }

    public int minOperations(int[] nums) {
        int sum = 0;

        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == 1){
                continue;
            }
            //0 后面跟着0, 合并为1次， 跟着1 合并为2次
            boolean next = false;
            int j = i+1;
            while (j<nums.length){
                if(nums[j] == 1){
                    next = true;
                    break;
                }
                j++;
            }
            i=j+1;

            if(next){
                sum += 2;
            }else {
                sum++;
            }
        }

        return sum;
    }

}
